﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kristall;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Kristall.Managers;

namespace Kristall.Screens
{
    public class MenuScreen : GameScreen
    {
        public List<MenuEntry> MenuEntries = new List<MenuEntry>();
        int selectedEntry = 0;
        public string Title = "";
        string selectedEntryChangedSound = "Select";
        string selectedSound = "Accept";

        public MenuScreen(string title = "")
        {
            this.Title = title;

            TransitionOnTime = TransitionOffTime = TimeSpan.FromSeconds(0.2f);
        }

        public override void HandleInput(InputState input, GameTime gameTime)
        {
            if (!IsActive)
                return;

            if (input.IsMenuDown)
            {
                selectedEntry++;

                Kristall.SoundManager.PlayNonInstancedSound(selectedEntryChangedSound);

                if (selectedEntry > MenuEntries.Count - 1)
                {
                    selectedEntry = 0;
                }
            }

            if (input.IsMenuUp)
            {
                selectedEntry--;

                Kristall.SoundManager.PlayNonInstancedSound(selectedEntryChangedSound);

                if (selectedEntry < 0)
                {
                    selectedEntry = MenuEntries.Count - 1;
                }
            }

            if (input.IsMenuSelect)
            {
                Kristall.SoundManager.PlayNonInstancedSound(selectedSound);

                MenuEntries[selectedEntry].Select();
            }

            if (input.IsMenuCancel)
            {
                OnCancel(this, null);
            }
        }

        public virtual void OnCancel(object sender, EventArgs e)
        {
            this.ExitScreen();
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch batch = ScreenManager.SpriteBatch;

            Vector2 position = new Vector2(100, 100);

            int padding = 20;

            batch.Begin();

            if (!string.IsNullOrEmpty(Title))
            {
                batch.DrawString(ScreenManager.LargeFont, Title, position + Vector2.One, Color.Black * TransitionAlpha);
                batch.DrawString(ScreenManager.LargeFont, Title, position, Color.White * TransitionAlpha);
                position.Y += ScreenManager.LargeFont.MeasureString(Title).Y + padding;
            }

            for (int i = 0; i < MenuEntries.Count; i++)
            {
                Color c = Color.White;

                if (selectedEntry == i)
                {
                    c = Color.Yellow;
                }

                batch.DrawString(ScreenManager.Font, MenuEntries[i].Text, position + Vector2.One, Color.Black * this.TransitionAlpha); //1px shadow
                batch.DrawString(ScreenManager.Font, MenuEntries[i].Text, position, c * this.TransitionAlpha);
                position.Y += ScreenManager.Font.MeasureString(MenuEntries[i].Text).Y + padding;
            }

            batch.End();

            //if (!IsActive)
            //{
            //    FadeBackBufferToBlack(0.33f);
            //}

            //base.Draw(gameTime);
        }
    }
}
